<template>
    <table border="1">
        <tr>
            <td>房型</td>
            <td>
                <select v-model="info.htid">
                    <option value="">--请选择--</option>
                    <option v-for="item in bind" :value="item.htid">{{ item.htname }}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>房间号码</td>
            <td>
                <input type="text" v-model="info.hnumber">
            </td>
        </tr>
        <tr>
            <td>房间描述</td>
            <td>
                <input type="text" v-model="info.prescribe">
            </td>
        </tr>
        <tr>
            <td>状态</td>
            <td>
                <input type="radio" v-model="info.hnstate" :value="true" name="A">上架
                <input type="radio" v-model="info.hnstate" :value="false" name="A">下架
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center;">
                <input type="button" value="保存" @click="addt">
            </td>
        </tr>
    </table>
    </template>
    <script setup lang="ts">
    import {ref} from 'vue'
    import axios from 'axios';
    import { onMounted } from 'vue';
    import { useRouter } from 'vue-router';
    onMounted(()=>{
        bindn();
    })
    const bind=ref([{
        "htid": "",
        "htname": "",
    }])
    const bindn=()=>{
        axios.get('http://localhost:41810/api/House/Bind_t')
        .then(res=>{
            bind.value=res.data
        })
        .catch(error=>{
            console.log(error);
        })
    }
    const info=ref({
        "hnumber": "",
        "prescribe": "",
        "hnstate": true,
        "htid": "",
        "hnisdel": 0
    })
    const rour=useRouter();
    const addt=()=>{
        if(info.value.hnumber==""){
            alert('房间号码不能为空');
            return;
        }
        if(info.value.htid==""){
            alert('房型不能为空');
            return;
        }
        if(info.value.prescribe==""){
            alert('房间描述不能为空');
            return;
        }
        axios.post('http://localhost:41810/api/House/AddNumber',info.value)
        .then(res=>{
            if(res.data>0){
                alert('保存成功');
                rour.push('/ShowNum');
            }
            else if(res.data==-1){
                alert('房间号码不能重复');
                return;
            }
            else{
                alert('保存失败');
            }
        })
        .catch(error=>{
            console.log(error);
        })
    }
    </script>